<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
 
</head>
<body>
  <div id="app">
    <button id="btn" @click="toggleBox">{{isShow?'隐藏':'显示'}}</button>
    <keep-alive>
      <common-head v-if="isShow"></common-head>
    </keep-alive>
  </div>
  <script src="./vue.global.js"></script>
  <script>
    const { createApp } = Vue;
    const app = createApp({
      data(){
        return {
          isShow: true
        }
      },
      methods: {
        toggleBox(){
          this.isShow = !this.isShow
        }
      }
    })
    app.component('CommonHead', {
      template: `
        <div>
          <h3>头部组件</h3>  
          <input type="text" v-model="msg"/>
          {{ msg }}
        </div>
      `,
      data() {
        return {
          msg: ''
        }
      },
      mounted(){
        console.log('组件初始化完成');
      },
      beforeUnmount(){
        console.log('组件卸载之前');
      }
    })
    app.mount('#app');
  </script>
</body>
</html>